package Compass;

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.io.Connector;
import javax.microedition.sensor.*;


/**
 *
 * @author thomas.s
 */
public class MenuBar implements DataListener {
    Image menubar;
    Image menudraw;
    int rgb[];
    int w,h;
    int y,a,mousex,mousey;
    double alpha;
    public boolean pushed,exitPushed,menuPushed,charged,feedback;
    SensorConnection batterySensor;
    int battery, oldbattery;



    MenuBar( String title )
    {
        rgb=null;
        alpha=0.5;
        exitPushed=false;
        menuPushed=false;
        pushed=false;
        oldbattery=0;
        battery=0;
        y=0;
        try {
            menubar = Image.createImage ( "/gui/menu_bar.png" );
            w = menubar.getWidth();
            h = menubar.getHeight();
        } catch (IOException e) {}
        rgb = new int[w*h];

        menudraw = Image.createImage(w,h);
        Graphics g = menudraw.getGraphics();
        g.drawImage(menubar,0,0,0);
        g.setFont(Font.getFont( Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM ));
        g.setColor(0xffffff);
        int th = g.getFont().getHeight();
        g.drawString(title,h+10, (h-th)/2, 0);
        menudraw.getRGB(rgb,0,w,0,0,w,h);
        
        SensorInfo[] sensors = SensorManager.findSensors(null,null);
        for ( int i=0; i<sensors.length; i++ )
        {
            if (sensors[i].getQuantity().equalsIgnoreCase("battery_charge"))
            {
               try {
                 batterySensor = (SensorConnection) Connector.open(sensors[i].getUrl());
                 batterySensor.setDataListener(this, 1);
               } catch (IOException e) { }
               break;
            }
        }

        
        blendMenuBar();
    }

    public void blendMenuBar()
    {
        if (rgb!=null)
        {
            // battery:
            int x0 = ((100-battery)*(202-186))/100+186;
            for ( int by=5; by<12; by++)
            for ( int bx=186; bx<202; bx++ ) rgb[bx+by*w]=bx<x0?0xf0f0f0:0x0060c0;


            a = (int)((alpha*255));
            if (a<0) a=0; else if (a>255) a=255; a<<=24;
            for ( int i=0; i<w*h; i++ ) rgb[i]=(rgb[i]&0xffffff)+a;
            menubar = Image.createRGBImage(rgb, w, h, true);
        }
    }

    public boolean update()
    {
        boolean old=charged, updated=false;
        charged=pushed && alpha>=1;
        if (!old && charged) feedback=true;
        
        menuPushed = (pushed && mousex<(h+10) && alpha>=1);
        exitPushed = (pushed && mousex>(w-(h+10)) && alpha>=1);
        if (battery!=oldbattery)
        {
            updated=true; oldbattery=battery;
        }
        if (pushed) { if (alpha<1.5) { alpha+=0.2; updated=true; } } else
        if (alpha>0.4) { alpha-=0.01; updated=true; }

        if (updated) blendMenuBar();
        return updated;
    }

    public void paint( Graphics g )
    {
        g.drawImage(menubar,0,y,0);
    }

    public boolean pointerPressed( int mx, int my )
    {
        mousex=mx; mousey=my;
        if (pushed = my<y+h) { return true; }
        return false;
    }

    public void pointerDragged( int mx, int my )
    {
        mousex=mx; mousey=my;
        pushed = my<y+h;
    }


    public void pointerReleased( int mx, int my )
    {
        if (pushed) { alpha=3; }
        menuPushed = exitPushed = pushed = false;
    }

    public void dataReceived( SensorConnection conn, Data[] arg, boolean lost )
    {
        battery = arg[0].getIntValues()[0];
    }


}
